package vista;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import modelo.Empleado;
import modelo.Servicio;
import modelo.Status;
import modelo.dao.Servidor;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class DlgSeleccionarServicio extends javax.swing.JDialog {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JButton btSalir;
	private JButton btAceptar;
	private JScrollPane jScrollPane1;
	private JTable tbServicios;
	private ArrayList<Servicio> servicios;

	/**
	 * Auto-generated main method to display this JDialog
	 */
	/*
	 * public static void main(String[] args) { SwingUtilities.invokeLater(new
	 * Runnable() { public void run() { JFrame frame = new JFrame();
	 * DlgSeleccionarEnPlanificacion inst = new
	 * DlgSeleccionarEnPlanificacion(frame); inst.setVisible(true); } }); }
	 */

	public DlgSeleccionarServicio() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				{
					btSalir = new JButton();
					getContentPane().add(btSalir);
					btSalir.setText("Salir");
					btSalir.setIcon(new ImageIcon(getClass().getClassLoader()
							.getResource("iconos/Salir.gif")));
					btSalir.setHorizontalTextPosition(JButton.CENTER);
					btSalir.setVerticalTextPosition(JButton.BOTTOM);
					btSalir.setBounds(313, 203, 79, 40);
				}
				{
					btAceptar = new JButton();
					getContentPane().add(btAceptar);
					btAceptar.setText("Aceptar");
					btAceptar.setIcon(new ImageIcon(getClass().getClassLoader()
							.getResource("iconos/Aceptar.png")));
					btAceptar.setHorizontalTextPosition(JButton.CENTER);
					btAceptar.setVerticalTextPosition(JButton.BOTTOM);
					btAceptar.setBounds(206, 203, 79, 40);
				}
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(36, 30, 336, 150);
					{
						AbstractTableModel tbServiciosModel = new AbstractTableModel() {
							@Override
							public Object getValueAt(int rowIndex, int columnIndex) {
								Servicio serv = servicios.get(rowIndex);
								
								switch (columnIndex) {
								case 0: return String.format("%04d", serv.getCodigo());
								case 1: return serv.getDescripcion();
								case 2: return serv.getTipoServicio().getDescripcion();
								}
								
								return null;
							}
							
							@Override
							public int getRowCount() {
								return servicios.size();
							}
							
							@Override
							public String getColumnName(int columnIndex) {
								switch (columnIndex) {
								case 0: return "Codigo";
								case 1: return "Descripcion";
								case 2: return "Tipo de servicio";
								}
								
								return null;
							}
							
							@Override
							public int getColumnCount() {
								return 3;
							}
						};

						tbServicios = new JTable();
						jScrollPane1.setViewportView(tbServicios);
						tbServicios.setModel(tbServiciosModel);
						tbServicios.setBounds(84, 100, 333, 104);
					}
				}
			}
			this.setSize(423, 299);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private DlgSeleccionarServicio dsser = this;
	private Servicio retorno;

	public Servicio mostrar() {
		setLocationRelativeTo(null);

		servicios = Servidor.servicios().consultarServiciosPorStatus(Status.ACTIVO);
		retorno = null;
		
		btAceptar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (tbServicios.getSelectedRow() != -1) {
					retorno = servicios.get(tbServicios.getSelectedRow());
					dsser.dispose();
				} else
					JOptionPane.showMessageDialog(dsser,
							"Debe Seleccionar un Servicio", "Seleccionar",
							JOptionPane.ERROR_MESSAGE);
			}
		});

		btSalir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dsser.dispose();
			}
		});

		setModalityType(ModalityType.APPLICATION_MODAL);
		setVisible(true);
		
		return retorno;
	}

}
